var xyTools = {
  // 取min-max之间的随机整数
  getRandomInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  },
  // 获取userId
  userId() {
    return uni.getStorageSync('userId');
  },
  // 是否为空
  isEmpty(obj) {
    if (typeof (obj) == 'string') {
      return obj == 'undefined' || obj == 'null' || obj.trim() == ''
    } else if (typeof (obj) == 'undefined') {
      return obj == undefined
    } else if (typeof (obj) == 'object') {
      if (obj == null) {
        return true
      }
      return Object.keys(obj).length ? false : true
    } else if (typeof (obj) == 'boolean') {
      return obj
    } return false
  },
  // 移除对象中没有值的key
  removeEmptyKeys(obj) {
    return Object.keys(obj).reduce((acc, key) => {
      if (this.isEmpty(obj[key])) {
        delete obj[key];
      }
    }, {});
  },
  safeAreaBottom() {
    const info = uni.getSystemInfoSync();
    return (info.safeAreaInsets.bottom ? info.safeAreaInsets.bottom : 10) + 'px';
  },
  // 处理富文本字符串,用于处理uni原生组件<rich-text>图片中间有间隙的问题
  handleRichTextStyle(richTexStr) {
    return richTexStr.replace(/\<p/gi, '<p style="display: flex;flex-direction: column;"').replace(/\<img/gi, '<img style="width:100%;height:auto;margin:0rpx;padding:0rpx;vertical-align:bottom;"')
  },
  // 微信支付
  wxPay(params, callback) {
    uni.requestPayment({
      provider: 'wxpay',
      timeStamp: params.timeStamp,
      nonceStr: params.nonceStr,
      package: params.package,
      signType: params.signType,
      paySign: params.paySign,
      success: (res) => {
        callback(res, null)
      },
      fail: (err) => {
        callback(null, err)
      }
    });
  },
  // 深拷贝对象
  deepClone(obj) {
    var target = {};
    for (var key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        if (typeof obj[key] === 'object' && obj[key] != null) {
          target[key] = xyTools.deepClone(obj[key]);
        } else {
          target[key] = obj[key];
        }
      }
    }
    return target;
  },
  // 序列化对象
  stringify(params) {
    const value = encodeURIComponent(JSON.stringify(params))
    return value
  },
  // 反序列化对象

  parse(params) {
    const value = JSON.parse(decodeURIComponent(params))
    return value
  },
  // 拨打电话
  makeCall(phone) {
    uni.makePhoneCall({
      phoneNumber: phone,
      success: (result) => {
        console.log(result);
      },
      fail: (error) => {
        console.log(error);
      }
    })
  }
}

export default xyTools